package com.dream.technology.controller;


import java.util.List;

import com.dream.technology.data.NoBody;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.dream.technology.components.riskControl.SymbolConstan;
import com.dream.technology.db.entity.RcFlow;
import com.dream.technology.db.entity.RcFlowItem;
import com.dream.technology.db.entity.RcFlowQuery;
import com.dream.technology.db.entity.vo.VoFlow;
import com.dream.technology.data.GenericRspDTO;
import com.dream.technology.service.RcFlowItemService;
import com.dream.technology.service.RcFlowService;
import com.dream.technology.vo.ErrorCode;

import io.swagger.annotations.Api;

@Api("风控子流程")
@RestController
@RequestMapping("/risk/admin/collection_flow")
public class RcFlowController {

    private static final Logger logger = LoggerFactory.getLogger(RcFlowController.class);

    @Autowired
    RcFlowService rcFlowService;
    @Autowired
    RcFlowItemService rcFlowItemService;


    /**
     * 风控子流程接口
     *
     * @param pageNum
     * @param pageSize
     * @param flowCode
     * @param flowName
     * @return
     */
    @GetMapping("/list")
    public GenericRspDTO<Object> getList(Integer pageNum, Integer pageSize, String flowName) {
        RcFlowQuery query = new RcFlowQuery();
        RcFlowQuery.Criteria criteria1 = query.createCriteria();
        RcFlowQuery.Criteria criteria2 = query.createCriteria();
        criteria1.andStatusEqualTo(false);
        criteria1.andFlowTypeEqualTo(SymbolConstan.FLOW_TYPE_SON);
        if (StringUtils.isNotBlank(flowName)){
            criteria1.andFlowNameLike("%" + flowName + "%");
            criteria2.andFlowCodeLike("%" + flowName + "%");
        }
        query.or(criteria2);
        query.setOrderByClause("enabled desc,create_date desc");
        PageHelper.startPage(pageNum, pageSize);
        List<RcFlow> rcFlowList = rcFlowService.selectByExample(query);
        PageInfo<RcFlow> pageInfo = new PageInfo<RcFlow>(rcFlowList);
        return GenericRspDTO.newInstance(ErrorCode.SUCCESS.getErrorCode(), ErrorCode.SUCCESS.getErrorMsg(), pageInfo);
    }

    /**
     * 风控子流程回显接口
     *
     * @param id
     * @return
     */
    @GetMapping("/id")
    public GenericRspDTO<VoFlow> getById(Long id) {

        RcFlow rcFlow = rcFlowService.selectByPrimaryKey(id);
        List<RcFlowItem> rcFlowItems = rcFlowItemService.selectByFlowId(id);
        VoFlow voFlow = new VoFlow();
        voFlow.setRcFlow(rcFlow);
        voFlow.setRcFlowItem(rcFlowItems);

        return GenericRspDTO.newInstance(ErrorCode.SUCCESS.getErrorCode(), ErrorCode.SUCCESS.getErrorMsg(), voFlow);
    }
    
    /**
     * 添加风控子流程
     * @param flowName
     * @param flowDetail
     * @param enabled
     * @return
     */
    @PostMapping("/add")
    public GenericRspDTO<Object> add(String flowName, String flowDetail, Boolean enabled) {
    	RcFlow record = new RcFlow();
    	record.setFlowName(flowName);
    	record.setFlowDetail(flowDetail);
    	record.setEnabled(enabled);
    	record.setStatus(false);
    	record.setFlowType(SymbolConstan.FLOW_TYPE_SON);
        rcFlowService.add(record);

        return GenericRspDTO.newInstance(ErrorCode.SUCCESS.getErrorCode(), ErrorCode.SUCCESS.getErrorMsg(), null);
    }

    /**
     * 配置风控子流程
     *
     * @param rcFlowStr
     * @return
     */
    @PostMapping("/config")
    public GenericRspDTO<Object> config(@RequestBody VoFlow rcFlowStr) {

        rcFlowService.updateByJsonStr(rcFlowStr);

        return GenericRspDTO.newInstance(ErrorCode.SUCCESS.getErrorCode(), ErrorCode.SUCCESS.getErrorMsg(), null);
    }

    /**
     * 编辑子流程
     * @param flowName
     * @param flowDetail
     * @param enabled
     * @return
     */
    @PostMapping("/modify")
    public GenericRspDTO<Object> modify(Long id, String flowName, String flowDetail, Boolean enabled) {
    	RcFlow record = new RcFlow();
    	record.setId(id);
    	record.setFlowName(flowName);
    	record.setFlowDetail(flowDetail);
    	record.setEnabled(enabled);
    	record.setStatus(false);
        rcFlowService.updateByPrimaryKeySelective(record);

        return GenericRspDTO.newInstance(ErrorCode.SUCCESS.getErrorCode(), ErrorCode.SUCCESS.getErrorMsg(), null);
    }
    
    /**
     * 修改配置风控子流程
     *
     * @param rcFlow
     * @return
     */
    @PostMapping("/update")
    public GenericRspDTO<Object> update(@RequestBody VoFlow rcFlow) {

        rcFlowService.updateByJsonStr(rcFlow);

        return GenericRspDTO.newInstance(ErrorCode.SUCCESS.getErrorCode(), ErrorCode.SUCCESS.getErrorMsg(), null);
    }

    /**
     * 删除风控子流程 (逻辑删除)
     *
     * @param id
     * @return
     */
    @PostMapping("/deleteOrStop")
    public GenericRspDTO<NoBody> delete(Long id, Boolean status, Boolean enabled) {
    	RcFlow flow = new RcFlow();
    	flow.setId(id);
        if (status != null) {
        	flow.setStatus(status);
		}
		if (enabled != null) {
			flow.setEnabled(enabled);
		}
        rcFlowService.updateByPrimaryKeySelective(flow);
        return GenericRspDTO.newInstance(ErrorCode.SUCCESS.getErrorCode(), ErrorCode.SUCCESS.getErrorMsg(), null);
    }


    /**
     * 风控规则集验证 转译为文本表达
     *
     * @param
     * @return
     */
    @PostMapping("/checkForDoc")
    public GenericRspDTO<Object> checkFlow( @RequestBody VoFlow flowExp) {
        String result = rcFlowService.checkFlow(flowExp);
        return GenericRspDTO.newInstance(ErrorCode.SUCCESS.getErrorCode(), ErrorCode.SUCCESS.getErrorMsg(), result);
    }

    /**
     * 风控流程检查引用，返回引用此子流程的总流程
     *
     * @param	flowId	子流程id
     * @return
     */
    @PostMapping("/checkRef")
    public GenericRspDTO<Object> checkFlowRef(Long flowId) {
    	List<RcFlow> result = rcFlowService.queryFlowByTag(3, flowId);
        return GenericRspDTO.newInstance(ErrorCode.SUCCESS.getErrorCode(), ErrorCode.SUCCESS.getErrorMsg(), result.size());
    }

    /**
     * 复制风控子流程
     * @param id
     * @param name 名称
     * @param code 编号
     * @return
     */
    @PostMapping("/copy")
    public GenericRspDTO<Object> copy(Long id, String flowName, String flowDetail, Boolean enabled) {
        rcFlowService.copy(id, flowName, flowDetail, enabled);
        return GenericRspDTO.newInstance(ErrorCode.SUCCESS.getErrorCode(), ErrorCode.SUCCESS.getErrorMsg(), null);
    }
    
    /**
     * 根据type获取用户标签、规则、规则集
     * @return
     */
    @GetMapping("/getChildItemByType")
    public GenericRspDTO<Object> getAllChildItem(Integer childItemType) {
    	return GenericRspDTO.newInstance(ErrorCode.SUCCESS.getErrorCode(), ErrorCode.SUCCESS.getErrorMsg(), rcFlowService.getAllChildItem(childItemType));
    }

    /**
     * 验证风控子流程名称是否重复
     * @param name
     * @return
     */
    @GetMapping("/getByName")
    public GenericRspDTO<Object> getByName(String name){
    	RcFlowQuery query = new RcFlowQuery();
    	RcFlowQuery.Criteria criteria = query.createCriteria();
    	criteria.andFlowNameEqualTo(name);
    	criteria.andFlowTypeEqualTo(SymbolConstan.FLOW_TYPE_SON);
    	int count = rcFlowService.selectByExample(query).size();
    	return GenericRspDTO.newInstance(ErrorCode.SUCCESS.getErrorCode(), ErrorCode.SUCCESS.getErrorMsg(), count);
    }

}
